<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class permission extends MY_Controller {    
    public function __construct()
    {
        parent::__construct();
        $this->lang->load('role');
        //$this->load->model('menu_model');
    }
    
    public function index()
    {
        $this->load->model('roles_model');
        if( !has_permission('System.Permission.PageView') ){
            redirect('no_access');
        }
        $this->template->set_page_title( lang('per_title_permission') );
        $this->template->set_view('permission/content_home');
        
        $this->template->render();
    }
    
    public function datatable_permission(){
        $this->load->model('roles_model');
        $aaData = $this->roles_model->load_datatable_permission($_GET,1);
        $totalData = $this->roles_model->load_datatable_permission($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function ajax_loadfrm_permission($id = null){
        $this->load->model('roles_model');
        $data = array();
        
        //$data['dvt'] = $this->vt_model->getAllDVT();
        //$data['category'] = $this->vt_model->getAllCategory();
        
        if($id != null){
            $data['permission'] = $this->roles_model->getPermissionById($id);
            $data['active'] = site_url('permission/edit');
        }else{
            $data['active'] = site_url('permission/add');
            $data['act'] = 'add';
        }
        
        $this->load->view($this->template->template.'/permission/form_add_permission',$data);
    }
    
    public function add(){
        $this->load->model('roles_model');
        
        if( !has_permission('System.Permission.Create') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $name = trim( $this->input->post('name') );
            $description = $this->input->post('description');
            $status = $this->input->post('status');
            
            $flag = true;
            $id = 0;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('per_error_01'),
                    'name' => 'name'
                );
            }else{
                $name_carr = explode(".", $name);
                if( count($name_carr) != 3 ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('per_error_03'),
                        'name' => 'name'
                    );
                }else{
                    if(trim($name_carr[0]) == '' || trim($name_carr[1]) == '' || trim($name_carr[2]) == ''){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('per_error_03'),
                            'name' => 'name'
                        );    
                    }else{
                        if( $this->roles_model->unique_permission($name) == false ){
                            $flag = false;
                            $error[] = array(
                                'message' => lang('per_error_02'),
                                'name' => 'name'
                            );
                        }
                    }
                    
                }
            }
            
            if($flag == true){
                $data_insert = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $status
                );
                
                $id = $this->roles_model->insertPermission($data_insert);
            }
            
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'id' => $id,
                'name' => $name,
                'description' => $description,
                'status' => $status
            );
            echo json_encode($data);
            
        }
    }
    public function edit(){
        $this->load->model('roles_model');
        if( !has_permission('System.Permission.Update') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $permission_id = $this->input->post('permission_id');
            $name = trim( $this->input->post('name') );
            $description = $this->input->post('description');
            $status = $this->input->post('status');
            
            $flag = true;
            $error = array();
            
            if( $name == '' ){
                $flag = false;
                $error[] = array(
                    'message' => lang('per_error_01'),
                    'name' => 'name'
                );
            }else{
                $name_carr = explode(".", $name);
                if( count($name_carr) != 3 ){
                    $flag = false;
                    $error[] = array(
                        'message' => lang('per_error_03'),
                        'name' => 'name'
                    );
                }else{
                    if(trim($name_carr[0]) == '' || trim($name_carr[1]) == '' || trim($name_carr[2]) == ''){
                        $flag = false;
                        $error[] = array(
                            'message' => lang('per_error_03'),
                            'name' => 'name'
                        );    
                    }else{
                        if( $this->roles_model->unique_permission_edit($permission_id,$name) == false ){
                            $flag = false;
                            $error[] = array(
                                'message' => lang('per_error_02'),
                                'name' => 'name'
                            );
                        }
                    }
                    
                }
            }
            
            if($flag == true){
                $data_update = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $status
                );
                $this->roles_model->updatePermission($data_update,$permission_id);
            }
            $permission = $this->roles_model->getPermissionById($permission_id);
            $html = '';
            if($permission['status'] == -1){
                $html = '<img src="'.base_url('template/ess/images/trash.png').'" alt="">';
            }elseif($permission['status'] == 1){
                $html = '<img src="'.base_url('template/ess/images/tick.png').'" alt="">';
            }elseif($permission['status'] == 2){
                $html = '<img src="'.base_url('template/ess/images/publish_x.png').'" alt="">';
            }
            $data = array(
                'flag' => $flag,
                'error' => $error,
                'action' => 'update',
                'id' => $permission_id,
                'name' => $permission['name'],
                'description' => $permission['description'],
                'status' => $html
            );
            echo json_encode($data);
            
        }
    }
    public function delete(){
        $this->load->model('roles_model');
        if( !has_permission('System.Permission.Delete') ){
            $data = array(
                'flag' => false,
                'error' => array('message' => 'No accsess!','name' => 'error')
            );
            echo json_encode($data);
            exit;
        }
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $id = $this->input->post('id');
            $data_update = array('status' => -1);
            $this->roles_model->updatePermission($data_update,$id);
            $data = array(
                'flag' => true,
                'message' => lang('per_msg_01'),
            );
            echo json_encode($data);
        }
    }
    
    /*
    function list_permission($page = 1)
    {
        $this->load->model('roles_model');
        
        $itemsPerPage=20;
        $space=5;
        if($page == 0){$page=1;}
        
        
        $start = ($page - 1) * $itemsPerPage;
        $items = $this->roles_model->getAllListPermission($itemsPerPage,$start);
        $totalItems = $this->roles_model->countAllListPermission();
        $totalPages = round($totalItems / $itemsPerPage);
        
        if( $totalItems%$itemsPerPage != 0 ){
            $totalPages = ($totalItems - ( $totalItems%$itemsPerPage ) ) /$itemsPerPage + 1;
        }
        
        $this->load->view('default/permission/ajax_list_permission',array(
            'items'=> $items,
            'itemsPerPage'=>$itemsPerPage,
            'page'=>$page,
            'totalPages'=>$totalPages,
            'space' => $space
        ));
    }
    
    function insert(){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Permission Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');

            }else{

                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $active = $this->input->post('active');
                
                $insertdata = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $active
                );
                $new_permissionId = $this->roles_model->insertPermission($insertdata);
                
                redirect('permission', 'location', 301);
            }
        }
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'permission'));
        $this->template->parse_view('content', 'default/permission/insert_permission', array(
            'action' => site_url('permission/insert'),
            'page_type' => 'insert'
        ));
       // Render template
        $this->template->render();
        
    }
    
    function edit($permissionId = 0){
        $this->load->model('roles_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        
        $permission = $this->roles_model->getPermissionById($permissionId);
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $this->form_validation->set_rules('name', 'Role Name', 'required|min_length[3]|max_length[100]|');
            $this->form_validation->set_rules('description', 'Description', 'required|max_length[255]|');
            
            if ($this->form_validation->run() == FALSE){
                $this->form_validation->set_error_delimiters('<div class="status error">
    <p class="closestatus"><a href="" title="Close">x</a></p><p><img SRC="img/icons/icon_error.png" alt="Error" /><span>Error!</span>', '</p></div>');
                
            }else{
                $name = $this->input->post('name');
                $description = $this->input->post('description');
                $active = $this->input->post('active');
                
                $updatetdata = array(
                    'name' => $name,
                    'description' => $description,
                    'status' => $active
                );
                $this->roles_model->updatePermission($permissionId,$updatetdata);
                
                redirect('permission', 'location', 301);
            }
        }
        
        $this->template->parse_view('sidebarleft','default/sidebarleft/sidebarleft',array('menu' => 'roles'));
        $this->template->parse_view('content', 'default/permission/insert_permission', array(
            'action' => site_url('permission/edit/'.$permissionId),
            'page_type' => 'edit',
            'permission'=>$permission
        ));
       // Render template
        $this->template->render(); 
    }

    function setPermission($roleId, $permissionId, $status){
        $this->load->model('roles_model');
        //echo $status;
        switch($status){
            case 'set':
                $data_insert = array('role_id'=>$roleId,'permission_id'=>$permissionId);
                $this->roles_model->insertRolePermission($data_insert);
            break;
            
            case 'unset':
                $this->roles_model->deleteRolePermission($roleId, $permissionId);
            break;
        }
        
        $this->load->view('default/roles/ajax_change_permission',array('role_id'=>$roleId,'permission_id'=>$permissionId,'status'=>$status));
    }
    */
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */